package network;

//import model.*;
import view.GameTable;
import network.ServerEvent;
import java.net.Socket;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.List;
import java.util.LinkedList;
import java.awt.event.ActionEvent;
import java.net.*;

public class AcolytePlayerClient extends Thread
{
  
  //protected AcolytePlayer user;
  protected String host = "localhost";
  protected int port = 4444;
  protected Socket connection;
  protected PrintWriter out;
  protected BufferedReader in;
  protected List<String> log;
  protected GameTable view;
  
  public AcolytePlayerClient(GameTable game)
  {
    log = new LinkedList<String>();
    view = game;
  }
  
  public void setHost(String newHost)
  {
    host = newHost;
  }
  
  public void setPort(int newPort)
  {
    port = newPort;
  }
  
  /**
   * establish a connection to a running game
   * return true if connection successfull
   * false otherwise
   **/
  protected boolean connect()
  {
    try
    {
      connection = new Socket(host, port);
      out = new PrintWriter(connection.getOutputStream(), true);
      in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      return true;
    }catch(IOException e)
    {
      return false;
    }
  }
  
  public List<String> getLog()
  {
    return log;
  }
  
  /**
   * send a message to the connected server
   * return true if message successfully sent
   * false otherwise
   **/
  public boolean message(String msg) throws IOException
  {
    if (msg != null) 
    {
      System.out.println("Client: " + msg);
      out.println(msg);
    }
    return true;
  }
  
  public void run()
  {
    if(connection == null)
    {
      connect();
    }
    while(true)
    {
      if(in != null)
      {
        String fromServer;
        try
        {
          fromServer = in.readLine();
          
          parseResponse(fromServer);
          //System.out.println("Server: " + fromServer);
          if(fromServer.equals("Bye."))
          {
            break;
          }
          
          } catch(IOException e)
          {
            e.printStackTrace();
          }
          }
    }
  }
  
  protected void parseResponse(String serverResponse)
  {
    log.add(serverResponse);
    System.out.println("Server: " + serverResponse);
    System.out.println("log: " + log);
    ServerEvent se = new ServerEvent(this, "_server");
    se.setArgumentString(serverResponse);
    view.actionPerformed(se);
    System.out.println(se.paramString());
  }
  
  public static void main(String[] args)
  {
    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    AcolytePlayerClient pc = new AcolytePlayerClient(new GameTable());
    if(pc.connect())
    {
      try
      {
        pc.start();
        String msg;
        while((msg = stdIn.readLine()) != null)
        {
          pc.message(msg);
          if(msg.equals("quit"))
            break;
        }
      }catch(IOException e)
      {
        e.printStackTrace();
      }
    }
    else
    {
      System.out.println("connection failed");
    }
  }
  
  
  }